﻿namespace PlugNT.Database.Common.Complex
{
    using MySql.Data.MySqlClient;
    using PlugNT.Safe;
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;
    using System.Data.SqlClient;

    public abstract class DbProvider
    {
        private static DbProviderFactory mysqlFactory;
        private static DbProviderFactory oleFactory;
        private static DbProviderFactory oracleFactory;
        private static DbProviderFactory sqlFactory;

        protected DbProvider()
        {
        }

        public static DbConnection CreateConnection(DbInfo dbinfo)
        {
            DbConnection connection = GetFactory(dbinfo).CreateConnection();
            connection.ConnectionString = dbinfo.ConnectionString;
            return connection;
        }

        public static DbProviderFactory GetFactory(DbInfo dbinfo)
        {
            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    if (sqlFactory == null)
                    {
                        sqlFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return sqlFactory;

                case "System.Data.OleDb":
                    if (oleFactory == null)
                    {
                        oleFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return oleFactory;

                case "System.Data.OracleClient":
                    if (oracleFactory == null)
                    {
                        oracleFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return oracleFactory;

                case "MySql.Data.MySqlClient":
                    if (mysqlFactory == null)
                    {
                        mysqlFactory = new MySqlClientFactory();
                    }
                    return mysqlFactory;
            }
            return null;
        }

        public static string GetSearchTableSql(DbInfo dbinfo, string TableName)
        {
            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    return ("select count(1) from sysobjects where id = object_id(N'[" + StringHelper.SqlFilter(TableName) + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1");

                case "System.Data.OleDb":
                    return ("SELECT Count(1) AS Qty FROM MSysObjects WHERE name='" + StringHelper.SqlFilter(TableName) + "'");

                case "System.Data.OracleClient":
                    return "";
            }
            return "";
        }

        public static string Help()
        {
            return "mailto:549002835@qq.com";
        }

        public static DbParameter MakeBitParam(DbInfo dbinfo, string paraName)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, 1);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.Bit, 1);
            }
            return new OleDbParameter(paraName, OleDbType.BigInt, 1);
        }

        public static DbParameter MakeCharParam(DbInfo dbinfo, string paraName, int size)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, size);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.Char, size);
            }
            return new OleDbParameter(paraName, OleDbType.WChar, size);
        }

        public static DbParameter MakeDateTimeParam(DbInfo dbinfo, string paraName)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, 12);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.DateTime);
            }
            return new OleDbParameter(paraName, OleDbType.Date);
        }

        public static DbParameter MakeIntParam(DbInfo dbinfo, string paraName)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, 3);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.Int);
            }
            return new OleDbParameter(paraName, OleDbType.Integer);
        }

        public static DbParameter MakeIntParam(DbInfo dbinfo, string paraName, int size)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, size);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.Int, size);
            }
            return new OleDbParameter(paraName, OleDbType.Integer, size);
        }

        public static DbParameter MakeTextParam(DbInfo dbinfo, string paraName)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, 0x2ef);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.NText);
            }
            return new OleDbParameter(paraName, OleDbType.VarWChar);
        }

        public static DbParameter MakeVarCharParam(DbInfo dbinfo, string paraName, int size)
        {
            DbParameter parameter = null;
            string dbType = dbinfo.DbType;
            if (dbType == null)
            {
                return parameter;
            }
            if (!(dbType == "System.Data.SqlClient"))
            {
                if (dbType != "System.Data.OleDb")
                {
                    if (dbType != "MySql.Data.MySqlClient")
                    {
                        return parameter;
                    }
                    return new MySqlParameter(paraName, size);
                }
            }
            else
            {
                return new SqlParameter(paraName, SqlDbType.VarChar, size);
            }
            return new OleDbParameter(paraName, OleDbType.VarWChar, size);
        }
    }
}

